﻿using Newtonsoft.Json;
using RPAVIEW.domain;
using RPAVIEW.util;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.EditPage.left {
    /// <summary>
    /// leftTree.xaml 的交互逻辑
    /// </summary>
    public partial class leftTree:UserControl {
        public leftTree() {
            InitializeComponent();
            InitTree();
        }

        public void InitTree(){
            Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RPAVIEW.resource.FunctionTree.json");
            using(BufferedStream bs = new BufferedStream(stream)) { 
                byte[] bytes = new byte[bs.Length];
                bs.Read(bytes, 0, bytes.Length);
                string JsonTree = Encoding.UTF8.GetString(bytes);
                List<TreeNodes>? tree = JsonConvert.DeserializeObject<List<TreeNodes>>(JsonTree);
                Dictionary<string,string> key2name = new Dictionary<string,string>();
                for(int i = tree.Count - 1; i >= 0; i--){
                    TreeNodes node = tree[i];
                    if(!key2name.ContainsKey(node.Key)) { 
                        key2name.Add(node.Key, node.Name);
                    }
                    if(!ExcludeEmpty(node.SubNodes, key2name)) { 
                        tree.Remove(node);
                    }
                }
                this.treeView.ItemsSource = tree;
                if(ParamUtil.GetAppGlobleVar<Dictionary<string,string>>(DefaultContents.FUNCTION_TREE_NAME) == null) { 
                    ParamUtil.SetAppGlobalVar(DefaultContents.FUNCTION_TREE_NAME, key2name);
                }
            }
        }

        private void BeginDragNode(object sender, MouseButtonEventArgs e) {
            TreeViewItem item = (TreeViewItem)sender;
            if(item.Items == null || item.Items.Count == 0) { 
                DragDrop.DoDragDrop(item, item ,DragDropEffects.Move);
            }
        }

        private bool ExcludeEmpty(List<TreeNodes> tree, Dictionary<string, string> key2name) {
            bool flag = false;
            for(int i = tree.Count - 1; i >= 0; i--) {
                TreeNodes node = tree[i];
                if(!key2name.TryGetValue(node.Key, out string value)) { 
                    key2name.Add(node.Key, node.Name);
                }
                if(node.Key.EndsWith("N") && string.IsNullOrEmpty(node.ItemInfo)) { 
                    tree.Remove(node);
                } else if(node.Key.EndsWith("N") && !string.IsNullOrEmpty(node.ItemInfo)) { 
                    flag = true;
                }
                if(node.Key.EndsWith("P")) { 
                    flag = ExcludeEmpty(node.SubNodes, key2name);
                    if(!flag) { 
                        tree.Remove(node);
                    }
                }
            }
            return flag;
        }
    }
}
